Imports System.Data

Partial Class WebControls_DataMine
    Inherits System.Web.UI.UserControl

    Dim strFilter As String = ""

#Region "Functions"
    Private Enum eDataType
        [Date] = 0
        [Integer] = 1
        [Double] = 2
        [String] = 3
    End Enum

    Private Sub PopulateFields()
        Dim dao As New DAOReport.VW_DATA_MINE_STRUC_DAO
        Dim vo As New DAOReport.VW_DATA_MINE_STRUC_VO

        Dim dt As DataTable = dao.Search(vo)

        ddlField.DataSource = dt
        ddlField.DataTextField = "COL_DESC"
        ddlField.DataValueField = "COL_ID"

        ddlField.DataBind()
        ddlField.Items.Insert(0, "")

        dt = Nothing
        dao = Nothing
        vo = Nothing
    End Sub

    Private Sub PopulateSign()
        ddlSign.Items.Clear()

        If ddlField.SelectedValue.ToString <> "" Then
            Select Case GetDataType(ddlField.SelectedValue.ToString)
                Case eDataType.Date, eDataType.Double, eDataType.Integer
                    ddlSign.Items.Add("=")
                    ddlSign.Items.Add(">")
                    ddlSign.Items.Add("<")
                Case eDataType.String
                    ddlSign.Items.Add("=")
                    ddlSign.Items.Add("like")
            End Select
        Else
            txtValue.Text = ""
        End If
        
    End Sub

    Private Sub SetValidation()
        txtValue.Attributes.Clear()

        If ddlField.SelectedValue.ToString <> "" Then
            Select Case GetDataType(ddlField.SelectedValue.ToString)
                Case eDataType.Date
                    txtValue.Attributes.Add("onblur", "CheckDate(this);")
                Case eDataType.Double, eDataType.Integer
                    Dim cf As New CIMSfunc

                    cf.setNumericFormat(txtValue)
                    cf = Nothing

            End Select
        End If
        
    End Sub

    Private Function GetDataType(ByVal fld_id As String) As eDataType
        Dim eReturn As eDataType = Nothing

        Dim dao As New DAOReport.VW_DATA_MINE_STRUC_DAO
        Dim vo As New DAOReport.VW_DATA_MINE_STRUC_VO

        vo.COL_ID = fld_id
        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            Select Case UCase(dt.Rows(0)("vb_type"))
                Case "DATE"
                    eReturn = eDataType.Date
                Case "INTEGER"
                    eReturn = eDataType.Integer
                Case "DOUBLE"
                    eReturn = eDataType.Double
                Case "STRING"
                    eReturn = eDataType.String
            End Select
        End If

        dt = Nothing
        dao = Nothing
        vo = Nothing

        Return eReturn
    End Function

#End Region

#Region "Properties"
    Public ReadOnly Property Filter() As String
        Get
            Dim strFilter As String = ""

            If ddlField.SelectedValue <> "" Then
                Select Case GetDataType(ddlField.SelectedValue)
                    Case eDataType.String
                        Dim dm As New DAOMain.CommonDAO

                        Select Case ddlSign.SelectedValue
                            Case "="
                                strFilter = ddlField.SelectedValue & " " & ddlSign.SelectedValue & " " & dm.Q(Trim(txtValue.Text))
                            Case "like"
                                strFilter = ddlField.SelectedValue & " " & ddlSign.SelectedValue & " " & dm.Q("%" & Trim(txtValue.Text) & "%")
                        End Select

                        dm = Nothing
                    Case eDataType.Double, eDataType.Integer
                        Dim cf As New CIMSfunc
                        strFilter = ddlField.SelectedValue & " " & ddlSign.SelectedValue & " " & cf.saveFormat(txtValue.Text)
                        cf = Nothing

                    Case eDataType.Date
                        Dim dm As New DAOMain.CommonDAO
                        Dim cf As New CIMSfunc

                        strFilter = ddlField.SelectedValue & " " & ddlSign.SelectedValue & " " & dm.Q(cf.ParseSetDate(txtValue.Text))
                        cf = Nothing
                        dm = Nothing
                End Select
            End If

            Filter = strFilter
        End Get
    End Property
#End Region

#Region "Form Events"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            PopulateFields()
        End If
    End Sub

    Protected Sub ddlField_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Call PopulateSign()
        Call SetValidation()
    End Sub

#End Region

End Class
